<?php namespace Home\Controller;
use Common\Model\Presonal;
use Common\Model\Address;
use Common\Model\Goods_order;
use Common\Model\Order_list;
//个人中心控制器
class PresonalController extends CommonController{
	public function index(){
		$cartData=Cart::getAllData();
		View::with('cartData',$cartData);
		//待付款订单
		$uid=$_SESSION['nickuser']['uid'];
		$orderModel=new Goods_order;
		$orderData=$orderModel
			->join('user_address','shop_user_address_uaid','=','uaid')
			->where("shop_user_uid={$uid}")
			->where("goorder=0")
			->get();
		$orderListModel=new Order_list;
		foreach($orderData as $k=>$v){
			$orderData[$k]['list']=$orderListModel
				->join('goods','shop_goods_gid','=','gid')
				->where("shop_goodsorder_goid={$v['goid']}")
				->get();
		}
		View::with('orderData',$orderData);
		View::make();
	}
	//显示全部订单
	public function order(){
		if(isset($_SESSION['nickuser'])){
			//显示全部订单
			$uid=$_SESSION['nickuser']['uid'];
			$orderModel=new Goods_order;
			$orderData=$orderModel
				->join('user_address','shop_user_address_uaid','=','uaid')
				->where("shop_user_uid={$uid}")
				->get();
			$orderListModel=new Order_list;
			foreach($orderData as $k=>$v){
				$orderData[$k]['list']=$orderListModel
						->join('goods','shop_goods_gid','=','gid')
						->where("shop_goodsorder_goid={$v['goid']}")->get();
			}
			View::with('orderData',$orderData);
//				p($orderData);
			View::make();
		}else{
			View::error('请登录后再查看订单');
		}
		
	}
	//个人信息
	public function presonal(){
		$userModel=new Presonal;
		if(IS_POST){
			if(!$userModel->edit()) View::error($userModel->getError());
			View::success('编辑成功',U('Presonal/index'));
		}
		$uid=$_SESSION['nickuser']['uid'];
		$data=$userModel->where("uid={$uid}")->find();
		View::with('data',$data);
		View::make();
	}
	//uploadify//上传头像
	public function upload()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
//	        p($file);
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
			
	    }
	}
	//uploadify异步删除图片
	public function delUpload(){
		$path=Q('post.path');
		unlink($path);
	}
	//收货地址
	public function address(){
		$addressModel=new Address;
		if(IS_POST){
			if(!$addressModel->store()) {
				View::error($addressModel->getError());
			}else{
				View::success('添加成功');
			}
		}
		$uid=$_SESSION['nickuser']['uid'];
		$addressData=$addressModel->where("shop_user_uid={$uid}")->get();
		View::with('addressData',$addressData);
		View::make();
	}
	//显示编辑收货地址
	public function editAddress(){
		$addressModel=new Address;
	    $uaid=Q('post.uaid');
		$addressData=$addressModel->where("uaid={$uaid}")->find();
		$addressData['address']=explode(',', $addressData['address']);
		$addressData['sheng']=$addressData['address'][0];
		$addressData['shi']=$addressData['address'][1];
		$addressData['xian']=$addressData['address'][2];
		$addressData['xx']=$addressData['address'][3];
		echo json_encode($addressData);
	}
	//编辑收货地址
	public function edit(){
		$addressModel=new Address;
		if(IS_POST){
			if(!$addressModel->edit()) View::error($addressModel->getErrr());
			View::success('编辑成功');
		}
	}
	//删除收货地址
	public function del(){
		$addressModel=new Address;
		$uaid=Q('get.uaid');
		$addressModel->where("uaid={$uaid}")->delete();
		View::success('删除成功');
	}
}
?>